home *** CD-ROM | disk | FTP | other *** search
- /*
- ** CurveLayer.c
- **
- ** These routines provide a layer of support between my bare-bones
- ** application skeleton and the Bezier curve code. There's little
- ** here of interest outside of the mouse tracking and the curve
- ** drawing.
- **
- ** David W. Smith
- */
-
- #include "QuickDraw.h"
- #include "MacTypes.h"
- #include "FontMgr.h"
- #include "WindowMgr.h"
- #include "MenuMgr.h"
- #include "TextEdit.h"
- #include "DialogMgr.h"
- #include "EventMgr.h"
- #include "DeskMgr.h"
- #include "FileMgr.h"
- #include "ToolboxUtil.h"
- #include "ControlMgr.h"
-
- /*
- * Tracker objects. Similar to MacAPP trackers, but much, much simpler.
- */
- struct Tracker
- {
- void (*track)();
- int thePoint;
- };
-
- static struct Tracker aTracker;
- static struct Tracker bTracker;
-
- /*
- * The Bezier curve control points.
- */
- Point control[4] = {{144,72}, {72,144}, {216,144}, {144,216}};
-
-
-
- /*
- * Draw
- *
- * Called from the skeleton to update the window. Draw the initial curve.
- */
- Draw()
- {
- PenMode(patXor);
-
- DrawTheCurve(control, true);
- }
-
-
- /*
- * DrawTheCurve
- *
- * Draw the given Bezier curve in the current pen mode. Draw the
- * control points if requested.
- */
- DrawTheCurve(c, drawPoints)
- Point c[];
- {
- if ( drawPoints )
- DrawThePoints(c);
-
- BezierCurve(c[0], c[1], c[2], c[3]);
- }
-
-
- /*
- * DrawThePoints
- *
- * Draw all of the control points.
- */
- DrawThePoints(c)
- Point c[];
- {
- int n;
-
- for ( n = 0 ; n < 4 ; ++n )
- {
- DrawPoint(c, n);
- }
- }
-
-
- /*
- * DrawPoint
- *
- * Draw a single control point
- */
- DrawPoint(c, n)
- Point c[];
- int n;
- {
- PenSize(3, 3);
- MoveTo(c[n].h - 1, c[n].v - 1);
- LineTo(c[n].h - 1, c[n].v - 1);
- PenSize(1, 1);
- }
-
-
- /*
- * GetTracker
- *
- * Produce a tracker object
- *
- * Called by the skeleton to handle mouse-down events.
- *
- * If the mouse touches a control point, return a tracker for that point.
- * Otherwise, return a tracker that drags a gray rectangle.
- */
- struct Tracker *
- GetTracker(point)
- Point point;
- {
- void TrackPoint(), TrackSelect();
- int i;
-
- aTracker.track = TrackPoint;
-
- for ( i = 0 ; i < 4 ; ++i )
- {
- if ( TouchPoint(control[i], point) )
- {
- aTracker.thePoint = i;
- return (&aTracker);
- }
- }
-
- bTracker.track = TrackSelect;
- return (&bTracker);
- }
-
-
- /*
- * TouchPoint
- *
- * Do the points touch?
- */
- #define abs(a) (a < 0 ? -(a) : (a))
-
- TouchPoint(target, point)
- Point target;
- Point point;
- {
- SubPt(point, &target);
-
- if ( abs(target.h) < 3 && abs(target.v) < 3 )
- return (1);
-
- return (0);
- }
-
-
- /*
- * TrackPoint
- *
- * Called while dragging a control point.
- */
- void
- TrackPoint(tracker, point, phase)
- struct Tracker *tracker;
- Point point;
- int phase;
- {
- Point savePoint;
-
- switch ( phase )
- {
- case 1:
- /* initial click - XOR out the control point */
- DrawPoint(control, tracker->thePoint);
- break;
-
- case 2:
- /* drag - undraw the original curve and draw the new one */
- DrawTheCurve(control, false);
- control[tracker->thePoint] = point;
- DrawTheCurve(control, false);
- break;
-
- case 3:
- /* release - redraw the control point */
- DrawPoint(control, tracker->thePoint);
- break;
- }
- }
-
-
- /*
- * TrackSelect
- *
- * Track a gray selection rectangle
- */
- static Point first;
- static Rect r;
-
- void
- TrackSelect(tracker, point, phase)
- struct Tracker *tracker;
- Point point;
- int phase;
- {
- switch ( phase )
- {
- case 1:
- PenPat(gray);
- first = point;
- SetupRect(&r, first, point);
- FrameRect(&r);
- break;
-
- case 2:
- FrameRect(&r);
- SetupRect(&r, first, point);
- FrameRect(&r);
- break;
-
- case 3:
- FrameRect(&r);
- PenPat(black);
- break;
- }
- }
-
- /*
- * SetupRect
- *
- * Setup the rectangle for tracking.
- */
- #define min(x, y) (((x) < (y)) ? (x) : (y))
- #define max(x, y) (((x) > (y)) ? (x) : (y))
-
- SetupRect(rect, point1, point2)
- Rect *rect;
- Point point1;
- Point point2;
- {
- SetRect(rect,
- min(point1.h, point2.h),
- min(point1.v, point2.v),
- max(point1.h, point2.h),
- max(point1.v, point2.v));
- }